<template>
  <div class="page-style">
    <btn-area @search="search" @download="downloadData" />
    <list-tab :table-data="agentList" @reloadFun="reloadFun" />
    <pagination :total="total" @pagination="paginationFun" />
  </div>
</template>

<script>
import btnArea from './components/agentList/agent-list-btn'
import listTab from './components/agentList/agent-list-tab'
import pagination from '@/components/Pagination'
import { getAgentList } from '@/api/agent'
import { getNow } from '@/utils/index'
export default {
  components: {
    pagination,
    btnArea,
    listTab
  },
  data() {
    return {
      total: 0,
      getData: {
        anent_name: '',
        brand_phone: '',
        status: '',
        p: 1,
        num: 10,
        brand_id: this.$store.getters.brand_id,
        level: ''
      },
      agentList: []
    }
  },
  created() {
    this.getAgentListFun()
  },
  methods: {
    paginationFun(val) {
      this.getData.p = val.page
      this.getData.num = val.limit
      this.getAgentListFun()
    },
    getAgentListFun() {
      getAgentList(this.getData).then(res => {
        console.log(res)
        if (res.list.length) {
          this.agentList = res.list
          this.total = Number(res.count)
        }
      })
    },
    search(val) {
      console.log(val)
      this.getData.brand_phone = val.brand_phone
      this.getData.anent_name = val.anent_name
      this.getAgentListFun()
    },
    reloadFun() {
      this.getAgentListFun()
    },
    downloadData() {
      const now = getNow()
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['后台登录账号', '授权名', '手机号', '微信号', '身份证号', '省份', '城市', '地区', '地址']
        const list = this.agentList
        const filterVal = ['anent_login_name', 'anent_name', 'brand_phone', 'anent_login_name', 'people_card_num', 'sheng', 'shi', 'qu', 'address']
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '代理列表' + now,
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
